//要想混入,必须是继承StateController,
//并且需要实现一个方法--判断是否网络环境变化后 是否需要加载数据

import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:get/get.dart';
import 'package:mall/utils/sc/state_controller.dart';


abstract class Reloadable {
  bool get isReloadable;
}

class NetworkChangeSC extends StateController {
  late final StreamSubscription<List<ConnectivityResult>> listen;
  final RxBool hasNetWork = false.obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    // if (UtilPlatform.isWeb) {
    //   NetworkChangeChecker.instance.onNetworkStatusChanged.listen((v) {
    //     if (v) {
    //       hasNetWork.value = true;
    //     } else {
    //       hasNetWork.value = false;
    //     }
    //   });
    // } else {
      //监听网络环境变化
      listen = Connectivity().onConnectivityChanged.listen((connectivityResult) {
        if (!connectivityResult.contains(ConnectivityResult.none)) {
          hasNetWork.value = true;
        } else {
          hasNetWork.value = false;
        }

        // Mobile network available.
        // } else if (connectivityResult.contains(ConnectivityResult.wifi)) {
        //   // Wi-fi is available.
        //   // Note for Android:
        //   // When both mobile and Wi-Fi are turned on system will return Wi-Fi only as active network type
        // } else if (connectivityResult.contains(ConnectivityResult.ethernet)) {
        //   // Ethernet connection available.
        // } else if (connectivityResult.contains(ConnectivityResult.vpn)) {
        //   // Vpn connection active.
        //   // Note for iOS and macOS:
        //   // There is no separate network interface type for [vpn].
        //   // It returns [other] on any device (also simulator)
        // } else if (connectivityResult.contains(ConnectivityResult.bluetooth)) {
        //   // Bluetooth connection available.
        // } else if (connectivityResult.contains(ConnectivityResult.other)) {
        //   // Connected to a network which is not in the above mentioned networks.
        // } else if (connectivityResult.contains(ConnectivityResult.none)) {
        //   // No available network types
        // }
      });
    // }
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    //取消监听网络
    listen.cancel();
  }
}
